package cn.yuanqiao.web.controller.cqbc;
/**
 * @author 赵常飞
 * @date 2024/6/13 15:29
 */

import cn.yuanqiao.common.core.controller.BaseController;
import cn.yuanqiao.common.core.domain.AjaxResult;
import cn.yuanqiao.common.core.page.TableDataInfo;
import cn.yuanqiao.common.annotation.Log;
import cn.yuanqiao.common.enums.BusinessType;
import cn.yuanqiao.cqbc.dataManage.domain.DataManage;
import cn.yuanqiao.cqbc.dataManage.service.IDataManageService;
import com.alibaba.fastjson2.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.util.List;

/**
 *@Author: 赵常飞
 *@Date: 2024/6/13 15:29
 *@Description:
 *@Note:
 */
@RestController
@RequestMapping("/cqbc/datamanager")
public class DataManageController extends BaseController {
    @Autowired
    private IDataManageService dataManageService;
    @Value("${MEMORYPOOL}")
    private String MEMORYPOOL;

    /**
     * 查询列表
     */
    //@RequiresPermissions("cqbc:datamanager:list")
    @GetMapping("/list")
    @Log(title = "查询接收检测列表", businessType = BusinessType.SELECT)
    public TableDataInfo list(DataManage dataManage)
    {
        startPage();
        List<DataManage> list = dataManageService.selectList(dataManage);
        return getDataTable(list);
    }
    @GetMapping("/getDataManagebyId/{id}")
    @Log(title = "根据id查询接收检测数据", businessType = BusinessType.SELECT)
    public AjaxResult getDataManagebyId(@PathVariable("id") Long id)
    {
        DataManage list = dataManageService.getDataManagebyId(id);
        return AjaxResult.success(list);
    }
    @PostMapping("/update")
    @Log(title = "根据id查询接收检测数据", businessType = BusinessType.SELECT)
    public AjaxResult update(@RequestBody DataManage dataManage)
    {
        int i = dataManageService.update(dataManage);
        return AjaxResult.success();
    }

    @DeleteMapping("/delete/{id}")
    @Log(title = "根据id删除接收检测数据", businessType = BusinessType.DELETE)
    public AjaxResult delete(@PathVariable("id") Long id)
    {
        int i = dataManageService.delete(id);
        return AjaxResult.success();
    }

    @PostMapping("/moveItem")
    @Log(title = "磁盘拷贝", businessType = BusinessType.OTHER)
    public AjaxResult moveItem(DataManage dataManage) {
        DataManage withId = dataManageService.getDataManagebyId(dataManage.getId());
        boolean flag = moveFileToPath(withId.getPath() + File.separator + withId.getName()+"."+withId.getFormat(), MEMORYPOOL);
        if (flag) {
            dataManage.setPath(MEMORYPOOL);
            //前端判断,为0是显示按钮
            dataManage.setMigrated("1");
            this.dataManageService.update(dataManage);
            dataManageService.updatedatable(withId.getId(),MEMORYPOOL);
        }else{
            return AjaxResult.error(500,"拷贝失败！");
        }
        return AjaxResult.success();
    }

    private boolean moveFileToPath(String newFile,String newPath){
        boolean flag = false;
        File startFile=new File(newFile);
        File endDirection=new File(newPath);
        if(!endDirection.exists()) {
            endDirection.mkdirs();
        }
        File endFile=new File(endDirection+ File.separator+ startFile.getName());
        try {
            if (startFile.renameTo(endFile)) {
                System.out.println("文件移动成功！目标路径：{"+endFile.getAbsolutePath()+"}");
                flag = true;
            } else {
                System.out.println("文件移动失败！起始路径：{"+startFile.getAbsolutePath()+"}");
            }
        }catch(Exception e) {
            System.out.println("文件移动出现异常！起始路径：{"+startFile.getAbsolutePath()+"}");
        }
        return flag;
    }
    @PostMapping("/handadd")
    @Log(title = "手动添加信息包", businessType = BusinessType.INSERT)
    public AjaxResult handadd(@RequestParam(value="categoryId")Long categoryId,@RequestParam(value="name")String name,@RequestParam(value="size")String size,
                              @RequestParam(value="receivingTime")String receivingTime,@RequestParam(value="format")String format,
                              @RequestParam(value="remark",required=false)String remark,@RequestParam(value="saveTime")String saveTime,
                              MultipartFile file)
    {
        int i = dataManageService.handadd(categoryId,name,size,receivingTime,format,remark,saveTime,file);
        return AjaxResult.success(i);
    }

}
